<?php
class ClientsController extends AppController {
    public $helpers = array('Html', 'Form', 'Session', 'Javascript');
    public $components = array('Session');
    public $uses = array('Client', 'CubiertaTerminada', 'ServiceOrderRowState','State','ServiceOrderRowRepaire','Repaire', 'Reparacione', 'User');
    

    public function index() {
        $this->set('clients', $this->Client->find('all'));
    }
    public function pdf($id = null, $selectedValues='') {
        App::import('Controller', 'ServiceOrderRowStates');
        App::import('Controller','ServiceOrderRowRepaires');
        //serv
        //App::import('ServiceOrderRowStates', 'Controller');
        //$posts = $this->requestAction(array('controller'=>'service_order_row_states','action'=>'ver'));
        
        if (!$id) {
            $this->Session->setFlash('Sorry, there was no PDF selected.');
            $this->redirect(array('action'=>'index'), null, true);
        }
        $selectedV = explode(',', $selectedValues);
        $this->Client->id = $id;
        $this->set('client', $this->Client->read());        
        $this->set('service_order_row_id', $selectedV);
        $fecha = date ('Y-m-d');     
        $this->set('cubierta_terminadas', $this->CubiertaTerminada->find('all',array('conditions' => array('id' => $selectedV))));
        $this->set('reparaciones', $this->Reparacione->find('all',array('conditions' => array('service_order_row_id' => $selectedV))));
        
        $sql = "INSERT INTO service_order_row_states (id ,service_order_row_id ,state_id,fecha,comentario)
                    VALUES (NULL ,  ".$selectedV[0].",  '8',$fecha, NULL);";
        $this->ServiceOrderRowState->query($sql);    
        $this->layout = 'pdf';
        $this->render();
    }
    public function view($id) {
        $this->Client->id = $id;
        $client = $this->Client->read();
        $this->set('client', $client);

        //busca si tiene asociado algun usuario registrado asociado a este cliente.
        $user = $this->User->find('first', array('conditions'=>array('codigo'=>$client['Client']['codigo'])));
        $this->set('user', $user);

        $this->set('cubierta_terminadas', $this->CubiertaTerminada->find('all',array('conditions' => array('clients_id' => $id, 'estado_cod' => 7, 'estado_cod' => 2 ))));
        $this->set('reparaciones', $this->Reparacione->find('all',array('conditions' => array('client_id' => $id))));
        //$sql = "SELECT * FROM service_order_row_repaires WHERE service_order_row_id = ".$selectedV[0];
        //$prueba = $this->ServiceOrderRowRepaire->query($sql);
    }
    
    public function edit($id = null) {
         $this->Client->id = $id;
    	if ($this->request->is('get')) {
    		$this->request->data = $this->Client->read();
    	} else {
    		if ($this->Client->save($this->request->data)) {
    			$this->Session->setFlash('Your post has been updated.');
    			$this->redirect(array('action' => 'index'));
    		} else {
    			$this->Session->setFlash('Unable to update your post.');
    		}
    	} 
    }
    
    public function add() {  
    	if ($this->request->is('post')) {
            $this->Client->create();
            if ($this->Client->save($this->request->data)) {
                $this->Session->setFlash('El cliente ha sido guardado.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al agregar el cliente.');
            }
        }
    }
    public function delete($id) {
    	//if ($this->request->is('get')) {
    	//	throw new MethodNotAllowedException();
    	//}
    	$this->Client->id = $id;
    	$this->set('client', $this->Client->delete($id));
    	$this->redirect(array('action' => 'index'));
    	if ($this->Client->delete($id)) {
    		$this->Session->setFlash('The client with id: ' . $id . ' has been deleted.');
    		$this->redirect(array('action' => 'index'));
    	}
    }
    
}